﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace imageprocessing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Bitmap image;
        string filename;
        List<int> value_histogram = new List<int>();
         int min=0;
         int max = 0;
        private void btx_open_Click(object sender, EventArgs e)
        {
            
            int[] histogram = new int[256];
            //listBox1.Items.Clear();
            value_histogram.Clear();
            OpenFileDialog file = new OpenFileDialog();
           // file.Filter = "file anh(*.jpg)|*.jpg";
            if (file.ShowDialog()==DialogResult.OK)
            {
                 image = new Bitmap(file
                    .FileName);
                 filename = file.FileName;
                pictureBox1.Image = image;
            }
            int a = 0;
            // dem so phan ru co muc sam nao do
            for (int i = 0; i < 256;i++ )
            {
                for (int j = 0; j < 256;j++ )
                {
                    a = (int)image.GetPixel(i, j).R;
                    histogram[(int)image.GetPixel(i, j).R] += 1;
                }
            }
            for (int k = 0; k < 255; k++)
            {
                value_histogram.Add(histogram[k]);
                //listBox1.Items.Add(histogram[k].ToString());
            }
            foreach (int histogram_Element in value_histogram)
            {
                Bitmap bitmap = new Bitmap(255, 200);
                Graphics graphics = Graphics.FromImage(bitmap);

                graphics.FillRectangle(new SolidBrush(Color.Black), 0, 0, 255, 200);
               
                //tim phan tu max
                for (int i = 0; i < value_histogram.Count; i++)
                {
                    if (max < value_histogram[i])
                    {
                        max = value_histogram[i];
                    }
                } 
                // tim phan tu min
                for (int i = 0; i < value_histogram.Count; i++)
                {
                    if (min > value_histogram[i])
                    {
                        min = value_histogram[i];
                    }
                }
              
                
                int n = 0;
                // ve do thi histogram
                for (int i = 0; i < value_histogram.Count; i++)
                {
                    if (value_histogram[i] == 0)
                    {
                        n = 100;
                    }
                    else
                    {
                        n = max / value_histogram[i];
                    }
                    graphics.DrawLine(Pens.Gray, i, 0, i, 100 - n);
                }
                graphics.DrawLine(Pens.Red, 127, 0, 127, 200);
                graphics.DrawLine(Pens.Red, 250, 0, 250, 200);
                graphics.DrawLine(Pens.Red, 200, 0, 200, 200);
                graphics.DrawLine(Pens.Red, 127, 200, 250, 0);
                pictureBox2.Image = bitmap;
                lb_min.Text = min.ToString();
                lb_max.Text = max.ToString();
            }
        }
       
      
        private void btx_histogram_Click(object sender, EventArgs e)
        {
        }

        private void btx_gey_image_Click(object sender, EventArgs e)
        {
            //for (int i = 0; i < image.Width-1;i++ )
            //{
            //    for (int j = 0; j < image.Height - 1;j++ )
            //    {
            //        Color getcolor = image.GetPixel(i, j);
            //       if (getcolor.R<127)
            //       {
            //           Color setcolor = Color.FromArgb(0, 0, 0);
            //           image.SetPixel(i, j, setcolor);
            //       }
            //        if (getcolor.R>=127)
            //        {
            //            Color setcolor = Color.FromArgb(255, 255, 255);
            //            image.SetPixel(i, j, setcolor);
            //        }
            //    }
            //}
            //grey_scale.Image = image;

            grey_scale.Image = lockbit(btx_gey_image.Text);
           // btx_gey_image.Enabled = false;
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            label2.Text = e.X.ToString();
            label4.Text = e.Y.ToString();
        }
        private Bitmap lockbit(string option)
        {
            Bitmap newbitmap = new Bitmap(filename);
            // Specify a pixel format.
            PixelFormat pxf = PixelFormat.Format32bppArgb;

            // Lock the bitmap's bits.
            Rectangle rect = new Rectangle(0, 0, newbitmap.Width, newbitmap.Height);
            BitmapData bmpData =
            newbitmap.LockBits(rect, ImageLockMode.ReadWrite,
                         pxf);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap.
            int numBytes = newbitmap.Width * newbitmap.Height * 4;
            byte[] rgbValues = new byte[numBytes];

            // Copy the RGB values into the array.
            Marshal.Copy(ptr, rgbValues, 0, numBytes);

            // Manipulate the bitmap, such as changing the
            // blue value for every other pixel in the the bitmap.
           if (option=="greyscale")
           {
               for (int counter = 0; counter < rgbValues.Length; counter += 4)
               {
                   if (rgbValues[counter] < 127)
                   {
                       //set value cho cac pixel cua vi tri thu 1,2,3 la cac pixel gan no
                       rgbValues[counter + 0] = 0;
                       rgbValues[counter + 1] = 0;
                      rgbValues[counter + 2] = 0;
                      rgbValues[counter + 3] = 0;
                   }
                   else
                   {
                       rgbValues[counter + 0] = 255;
                       rgbValues[counter + 1] = 255;
                      rgbValues[counter + 2] = 255;
                      rgbValues[counter + 3] = 255;
                   }
               }
               
           }
           else if (option=="grey image")
           {
               for (int counter = 0; counter < rgbValues.Length; counter += 4)
               {
                   
                       //set value cho cac pixel cua vi tri thu 1,2,3 la cac pixel gan no
                   int x = (int)rgbValues[counter];
                   int y = (int)rgbValues[counter+1];
                 int z= (int)rgbValues[counter+2];
                 int k = (int)rgbValues[counter + 3];
                  int trungbinh = (x+ y +z+k) / 4;
                  rgbValues[counter + 0] = (Byte)trungbinh;
                  rgbValues[counter + 1] = (Byte)trungbinh;
                  rgbValues[counter + 2] = (Byte)trungbinh;
               }
           }

            // Copy the RGB values back to the bitmap
            Marshal.Copy(rgbValues, 0, ptr, numBytes);

            // Unlock the bits.
            newbitmap.UnlockBits(bmpData);
            return newbitmap;
        }

        private void btx_grey_image_Click(object sender, EventArgs e)
        {

            grey_scale.Image = lockbit(btx_grey_image.Text);
            //btx_grey_image.Enabled = false;
        }

        private void btx_save_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            if (save.ShowDialog()==DialogResult.OK)
            {
                Bitmap image = lockbit("grey image");
                image.Save(save.FileName, ImageFormat.Jpeg);
            }
        }

        private void numeric_min_ValueChanged(object sender, EventArgs e)
        {
            pictureBox1.Image = changer_imagehistogram((Bitmap)pictureBox1.Image, (int)numeric_min.Value,"min");
            if (numeric_min.Value+10>255)
            {
                numberric_max.Value = 255;
                
            }
            else
            {
                numberric_max.Value = numeric_min.Value + 10;
            }
        }
        private Bitmap changer_imagehistogram(Bitmap image,int values,string option)
        {
             Bitmap newbitmap = image;
            // Specify a pixel format.
            PixelFormat pxf = PixelFormat.Format32bppArgb;
            
            // Lock the bitmap's bits.
            Rectangle rect = new Rectangle(0, 0, newbitmap.Width, newbitmap.Height);
            BitmapData bmpData =
            newbitmap.LockBits(rect, ImageLockMode.ReadWrite,
                         pxf);

            // Get the address of the first line.
            IntPtr ptr = bmpData.Scan0;

            // Declare an array to hold the bytes of the bitmap.
            int numBytes = newbitmap.Width * newbitmap.Height * 4;
            byte[] rgbValues = new byte[numBytes];

            // Copy the RGB values into the array.
            Marshal.Copy(ptr, rgbValues, 0, numBytes);

            // Manipulate the bitmap, such as changing the
            // blue value for every other pixel in the the bitmap.
           
               for (int counter = 0; counter < rgbValues.Length; counter += 4)
               {
                  if (option=="min")
                  {
                      if (rgbValues[counter] < ((byte)values))
                      {
                          //set value cho cac pixel cua vi tri thu 1,2,3 la cac pixel gan no
                          rgbValues[counter + 0] = (byte)values;

                      }
                  }
                  else if (option=="max")
                  {
                      if (rgbValues[counter] > ((byte)values))
                      {
                          rgbValues[counter + 0] = (byte)values;
                      }
                  }
                  

               }
                // Copy the RGB values back to the bitmap
            Marshal.Copy(rgbValues, 0, ptr, numBytes);

            // Unlock the bits.
            newbitmap.UnlockBits(bmpData);
            return newbitmap;
        }

        private void numberric_max_ValueChanged(object sender, EventArgs e)
        {
            pictureBox1.Image = changer_imagehistogram((Bitmap)pictureBox1.Image, (int)numberric_max.Value, "max");
            if (numberric_max.Value-10>0)
            {
                numeric_min.Value = numberric_max.Value - 10;
            }
            else
            {
                numeric_min.Value = 0;
            }
        }
          
          

           
        

      
    }
}
